package com.hfut.eeg.common.util;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.system.SystemUtil;
import com.hfut.eeg.dto.LoginParam;

import java.beans.PropertyDescriptor;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 梁琦
 * @program: eeg
 * @Package com.hfut.eeg.common.util
 * @Description: 时间格式归一
 * @date 2019-12-03 15:14
 */
public class GMT {
    public static final int EPOCH_YEAR = 1970;
    public static final int[][] MONTH_DAYS = {
            {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
            {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };
    public static final long MSECS_DAY = 1000*3600*24L;

    private long timestamp;
    private int mil;
    private int sec;
    private int min;
    private int hour;
    private int wday;
    private int mday;
    private int yday;
    private int mon;
    private int year;

    public GMT(long timestamp, long shift) {
        this.timestamp = timestamp + shift;
        long dayclock = (this.timestamp % MSECS_DAY) / 1000L;
        long dayno = this.timestamp / MSECS_DAY;

        mil = (int) (this.timestamp % 1000L);
        sec = (int) (dayclock % 60);
        min = (int) ((dayclock % 3600) / 60);
        hour = (int) (dayclock / 3600);
        wday = (int) ((dayno + 4) % 7);
        while (dayno >= yearDays(EPOCH_YEAR + year)) {
            dayno -= yearDays(EPOCH_YEAR + year);
            year++;
        }
        year = EPOCH_YEAR + year;
        yday = (int)dayno;
        int[] monthDays = leapYear(year) ? MONTH_DAYS[1] : MONTH_DAYS[0];
        while (dayno >= monthDays[mon]) {
            dayno -= monthDays[mon];
            mon++;
        }
        mon++;
        mday = (int)dayno + 1;
    }

    public long toLongInteger() {
        return year     * 10000000000000L
                + mon   * 100000000000L
                + mday  * 1000000000L
                + hour  * 10000000L
                + min   * 100000L
                + sec   * 1000L
                + mil;
    }

    private static int yearDays(int year) {
        return leapYear(year) ? 366 : 365;
    }

    private static boolean leapYear(int year) {
        return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
    }

    public int getMil() { return mil; }
    public void setMil(int mil) { this.mil = mil; }
    public int getSec() { return sec; }
    public void setSec(int sec) { this.sec = sec; }
    public int getMin() { return min; }
    public void setMin(int min) { this.min = min; }
    public int getHour() { return hour; }
    public void setHour(int hour) { this.hour = hour; }
    public int getWday() { return wday; }
    public void setWday(int wday) { this.wday = wday; }
    public int getMday() { return mday; }
    public void setMday(int mday) { this.mday = mday; }
    public int getYday() { return yday; }
    public void setYday(int yday) { this.yday = yday; }
    public int getMon() { return mon; }
    public void setMon(int mon) { this.mon = mon; }
    public int getYear() { return year; }
    public void setYear(int year) { this.year = year; }

   static int[] calculateMaxMatchLengths(String pattern) {
        int[] maxMatchLengths = new int[pattern.length()];
        int maxLength = 0;
        for (int i = 1; i < pattern.length(); i++) {
            while (maxLength > 0 && pattern.charAt(maxLength) != pattern.charAt(i)) {
                maxLength = maxMatchLengths[maxLength - 1]; // ①
            }
            if (pattern.charAt(i) == pattern.charAt(maxLength)) {
                maxLength++; // ②
            }
            maxMatchLengths[i] = maxLength;
        }
        return maxMatchLengths;
    }




    public static void main(String[] args) {
//        long start = System.currentTimeMillis();
//        int total = 500000;
//        for (int i = 0; i < total; i ++) {
//            GMT gmt = new GMT(System.currentTimeMillis(), 1000L*3600*8);
//            System.out.println(gmt.toLongInteger());
//        }
//        long duration = System.currentTimeMillis() - start;
//        System.out.println("Total: " + duration + "ms, " + total/duration + "/ms");
        //System.out.println(RuntimeUtil.execForStr("ipconfig"));
        PropertyDescriptor[] propertyDescriptors = BeanUtil.getPropertyDescriptors(LoginParam.class);
        for(PropertyDescriptor propertyDescriptor:propertyDescriptors){
            propertyDescriptor.getName();
        }
        Console.log(SystemUtil.getHostInfo());
        Console.log(calculateMaxMatchLengths("ABCDABD"));
    }
}
